package com.suredy;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.commons.io.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * property配置加载器
 * <p>
 * 配置文件默认路径为项目根目录
 * 
 * @author VIVID.G
 * @since 2015-4-14
 * @version v0.1
 */
@Component
public class PropertiesLoader {

	private final static Logger log = LoggerFactory.getLogger(PropertiesLoader.class);

	/* 配置文件名称 */
	private Set<String> PROPERTIES_FILE_NAMES = new HashSet<String>();
	/* 配置缓存 */
	private final static Properties PROPERTIES = new Properties();

	private PropertiesLoader() {
		this.PROPERTIES_FILE_NAMES.add("sys.properties");
	}

	@PostConstruct
	protected void init() {
		Reader reader = null;

		for (String fileName : this.PROPERTIES_FILE_NAMES) {
			if (StringUtils.isBlank(fileName)) {
				log.error("Properties file name is blank. Please check it now.");
				continue;
			}

			reader = new InputStreamReader(this.getClass().getResourceAsStream("/" + fileName), Charsets.ISO_8859_1);

			try {
				PROPERTIES.load(reader);
			} catch (IOException e) {
				log.error("Load properties error.", e);
			} finally {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}

		// 释放掉这个无用的内存
		this.PROPERTIES_FILE_NAMES.clear();
		this.PROPERTIES_FILE_NAMES = null;
	}

	@PreDestroy
	protected void destroy() {
		PROPERTIES.clear();
	}

	/**
	 * 获取一个属性值
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static String getProperty(String key, String defValue) {
		if (StringUtils.isBlank(key))
			return defValue;

		String val = PROPERTIES.getProperty(key);

		if (StringUtils.isBlank(val))
			return defValue;

		return val;
	}

	public static String getString(String key, String defValue) {
		return getProperty(key, defValue);
	}

	public static String getString(String key) {
		return getString(key, null);
	}

	public static Integer getInteger(String key, Integer defValue) {
		String val = getString(key);

		if (StringUtils.isBlank(val))
			return defValue;

		try {
			return Integer.parseInt(val);
		} catch (NumberFormatException e) {
			return defValue;
		}
	}

	public static Integer getInteger(String key) {
		return getInteger(key, null);
	}

	public static Float getFloat(String key, Float defValue) {
		String val = getString(key);

		if (StringUtils.isBlank(val))
			return defValue;

		try {
			return Float.parseFloat(val);
		} catch (NumberFormatException e) {
			return defValue;
		}
	}

	public static Float getFloat(String key) {
		return getFloat(key, null);
	}

	public static Boolean getBoolean(String key, Boolean defValue) {
		String val = getString(key);

		if (StringUtils.isBlank(val))
			return defValue;

		try {
			return Boolean.parseBoolean(val);
		} catch (NumberFormatException e) {
			return defValue;
		}
	}

	public static Boolean getBoolean(String key) {
		return getBoolean(key, null);
	}

}
